#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "cio.h"
#include "kinect.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(&kreader,SIGNAL(dataNotification()),this,SLOT(kinectData()));
    connect(&reproductor,SIGNAL(dataNotification()),this,SLOT(kinectData2()));
    //connect(&kreader,SIGNAL(statusNotification(Kinect)),this,SLOT(kinectStatus(Kinect::KinectStatus)));
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::kinectData()
{

   QImage img = kreader.getDepth();
   QImage img2= kreader.getCamera();
   ui->label->setPixmap(QPixmap::fromImage(img));
   //img = kreader.getCamera();
   ui->label_2->setPixmap(QPixmap::fromImage(img2));
   xnFPSMarkFrame(&xnFPS);
   /*ui->labelImage->setPixmap(QPixmap::fromImage(img));
   QKinect::Bodies bodies = kreader.getBodies();
   sbKinectNumBody->setText(QString("Body: %1").arg(bodies.size()));

   sbKinectFPS->setText(QString("FPS: %1").arg(xnFPSCalc(&xnFPS)));

   printf("Available bodies: ");
   for(unsigned i=0;i<bodies.size();i++)
      if(bodies[i].status == QKinect::Tracking)
         printf("%d ",i);
   printf("\n");
    */

}
void MainWindow::kinectData2()
{

   QImage img = reproductor.getDepth();
   QImage img2= reproductor.getCamera();
   ui->label->setPixmap(QPixmap::fromImage(img));
   ui->label_2->setPixmap(QPixmap::fromImage(img2));


}
void MainWindow::kinectCalibration(unsigned id, Kinect::CalibrationStatus s)
{
   printf("User %u: Calibration ",id);
   if(s==Kinect::CalibrationStart)
      printf("started\n");
   if(s==Kinect::CalibrationEndSuccess)
      printf("successful\n");
   if(s==Kinect::CalibrationEndFail)
      printf("failed\n");
}
void MainWindow::kinectStatus(Kinect::KinectStatus s)
{

   QString str("Kinect: ");
   if(s==Kinect::Idle)
      str += "Idle";
   if(s==Kinect::Initializing)
      str += "Initializing";
   if(s==Kinect::OkRun)
      str += "Running";
   if(s==Kinect::ErrorStop)
      str += "Error";
    //sbKinectStatus.setText(str);
   if(s==Kinect::ErrorStop || s==Kinect::OkRun || s==Kinect::Idle)
      ui->pushButton->setEnabled(true);
   if(s==Kinect::OkRun)
      ui->pushButton->setText("Stop Kinect");
   if(s==Kinect::ErrorStop || s==Kinect::Idle)
      ui->pushButton->setText("Start Kinect");


}

void MainWindow::on_pushButton_clicked()
{
    ui->pushButton->setEnabled(false);
    if(kreader.isRunning())
    {
       kreader.stop();
    }
    else
    {
       kreader.start();
       xnFPSInit(&xnFPS, 180);
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    ui->pushButton_2->setEnabled(false);
    if(reproductor.isRunning())
    {
       reproductor.stop();
    }
    else
    {
        QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"",tr("Files (*.oni*)"));
        reproductor.start(fileName);
    }
}
